package com.heisnenhub.common.module.exception;

import com.heisnenhub.common.module.constant.HttpConstant;
import com.heisnenhub.common.module.enmus.ApiReturnEnums;
import lombok.Data;

@Data
public class RRException extends RuntimeException {

    private Integer code = null;

    private String msg = null;

    public RRException(String msg) {
        code = 0;
        setMsg(msg);
    }

    public RRException() {
    }

    public RRException(Integer ecode, String emsg) {
        this.code = ecode;
        this.msg = emsg;
    }


    public RRException(ApiReturnEnums apiReturnEnums) {
        super(apiReturnEnums.getDesc());
        this.code = apiReturnEnums.getCode();
        this.msg = apiReturnEnums.getDesc();
    }


    /**
     * @param msg
     * @param formatstr
     * @return
     */
    public static RRException rrExceptionformatByCode(Integer ecode, String msg, Object... formatstr) {
        RRException plEx = new RRException();
        plEx.setCode(ecode);
        plEx.setMsg(String.format(msg, formatstr));
        return plEx;
    }

    /**
     * %s  转换
     *
     * @param msg
     * @param formatstr
     * @return
     */
    public static RRException rrException(String msg, Object... formatstr) {
        RRException plEx = new RRException();
        plEx.setCode(HttpConstant.HttpCode.SC_FAILE);
        plEx.setMsg(String.format(msg, formatstr));
        return plEx;
    }


    @Override
    public String getMessage() {
        return msg;
    }
}